package com.wkg.lotterysystem.dao.mapper;

import com.wkg.lotterysystem.dao.dataobject.Encrypt;
import com.wkg.lotterysystem.dao.dataobject.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * 查询邮箱绑定人数
     *
     * @param email
     * @return
     */
    @Select("select count(*) from user where email = #{email}")
    int countByMail(@Param("email") String email);

    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    @Insert("insert into user(user_name,email,phone_number,password,identity) "
            + "values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insert(UserDO userDO);

    @Select("select * from user where email = #{email}")
    UserDO selectByMail(@Param("email") String email);

    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectByPhone(@Param("phoneNumber") Encrypt phoneNumber);


    @Select(" <script>" +
            " SELECT * FROM user " +
            " <if test= \"identity!=null\" >" +
            " WHERE identity = #{identity} " +
            " </if>" +
            " ORDER BY id DESC" +
            " </script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identity);

    @Select("<script>" +
            " SELECT id FROM user " +
            " WHERE id IN " +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            "  #{item}" +
            " </foreach>" +
            " </script>")
    List<Integer> selectExistByIds(@Param("items") List<Integer> id);

    @Select("<script>" +
            " SELECT * FROM user " +
            " WHERE id IN " +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            "  #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items")List<Integer> ids);
}
